import java.io.*;
import java.util.Scanner;

class Account implements Serializable{
    int accountNumber;
    String firstName;
    String lastName;
    double accountBalance;
    
    public Account() {
    }
    
    public Account(int accNo, String fName, String lName, double accBa) {
        this.accountNumber = accNo;
        this.firstName = fName;
        this.lastName = lName;
        this.accountBalance = accBa;
    }
    
    
    public void deposit(double amt) {
        this.accountBalance += amt;
    }
    
    public void withdraw(double amt) {
        this.accountBalance -= amt;
    }
    
    
    public int getAccountNumber() {
        return accountNumber;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public String getLastName() {
        return lastName;
    }
    
    public double getAccountBalance() {
        return accountBalance;
    }
    
    public void setAccountNumber(int accountNumber) {
        this.accountNumber = accountNumber;
    }
    
    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    public String toString() {
        return "[" + accountNumber + ", " + firstName + ", " + lastName + ", " + accountBalance + "]";
    }
    
}

class Bank {
    static int lastAccountNumber=0;
    static String fileName = "Accounts.bin";
    Account[] accounts;
    Scanner in = new Scanner(System.in);

    public Bank() {
        accounts = new Account[100];
        File f = new File(fileName);
        if(!f.exists()) {
            return;
        }
        try {
            FileInputStream fStr = new FileInputStream(f);
            ObjectInputStream oStr = new ObjectInputStream(fStr);
            Account acc = null;
            while((acc = (Account)oStr.readObject()) != null) {
                accounts[lastAccountNumber++] = acc ;
            }
            oStr.close();
            fStr.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println("Tong so Acc khoi tao: " + lastAccountNumber);
    }

    public void creatAccount() {
        if(lastAccountNumber >100) {
           System.out.println("Danh sach full"); 
        }
        String fName="", lName="";
        double balance=0;
        do {
            System.out.print("Nhap ten cua ban: ");
            fName = in.next();
            if(fName.equals("")) {
                System.out.println("Ten ko duoc de trong!");
            }
        } while(fName.equals(""));

        do {
            System.out.print("Nhap ho cua ban: ");
            lName = in.next();
            if(lName.equals("")) {
                System.out.println("Ho ko duoc de trong!");
            }
        } while(lName.equals(""));
        
        try { 
            do {
                System.out.print("Nhap so tien muon goi: ");
                balance = in.nextDouble();
                if(balance <= 100) {
                    System.out.println("Tien nhap vao phai lon hon 100!");
                }
            } while(lName.equals(""));
            
            Account acc = new Account(lastAccountNumber+1, fName, lName, balance);
            accounts[lastAccountNumber++] = acc;
            System.out.println("Tao tai khoan thanh cong!");
        } catch(Exception e) {
            System.out.println("Balance phai la so!");
            in.next();
            return;
        }
        
        try {
            FileOutputStream f = new FileOutputStream(fileName);
            ObjectOutputStream oStr = new ObjectOutputStream(f);
            for(int i=0;i < this.lastAccountNumber;i++) {
                oStr.writeObject(accounts[i]);
            }
            f.close();
            oStr.close();
        } catch(Exception e) {
        
        }
    }
    private Account find (int number){
        for(int i=0;i<lastAccountNumber;i++){
            if(accounts[i].getAccountNumber() == number) {
                return accounts[i];
            }
        }
        return null;
    }

    public void withdraw(){
        try{
            int accNo;
            System.out.print("Nhap so tai khoan se rut: ");
            accNo = in.nextInt();
            Account acc = find(accNo);
            if(acc == null) {
                System.out.println("Tai khoan nay khong ton tai!");
            } else {
                double amount;
                System.out.print("Nhap so tien se rut: ");
                amount = in.nextDouble();
                if(amount > acc.getAccountBalance()-500) {
                    System.out.println("So tien rut sai quy dinh");
                } else if (amount <0) {
                    System.out.println("So tien rut phai lon hon 0!");
                } else {
                    acc.withdraw(amount);
                    System.out.println("Rut tien thanh cong!");
                }
            }
        } catch(Exception e) {
            System.out.print("Loi nhap so!");
            in.next();
        }
    }
    
    public void deposit() {
        int accNo;
        try {
            System.out.print("Nhap so tai khoan can gui: ");
            accNo = in.nextInt();
            Account acc = find(accNo);
            if(acc == null) {
                System.out.println("Tai khoan khong ton tai!");
            } else {
                double amount;
                System.out.print("Nhap so tien can gui: ");
                amount = in.nextDouble();
                if(amount < 0) {
                    System.out.println("So tien gui phai lon hon 0!");
                } else {
                    acc.deposit(amount);
                    System.out.println("Gui tien thanh cong!");
                }
            }
        } catch(Exception e) {
            System.out.println("Loi nhap so. Yeu cau nhap lai!");
            in.next();
        }
    }
    
    public void display() {
        int accNo ;
        try {
            System.out.print("Nhap so tai khoan can xem: ");
            accNo = in.nextInt();
            Account acc = find(accNo);
            if(acc == null) {
                System.out.println("Tai khoan khong ton tai");
            } else {
                System.out.println(acc.toString());
            }
        } catch(Exception e) {
            System.out.println("Loi nhap so. Yeu cau nhap lai");
            in.next();
        }
    }
}

class BankTest {
    
    public BankTest() {
        
    }
    public static void main(String[] args ){
        int option ;
        Bank bank = new Bank();
        Scanner in = new Scanner(System.in);
        try {
            do {
                System.out.println(" *** MENU ***");
                System.out.println("1. Tao tai khoan moi");
                System.out.println("2. Rut tien");
                System.out.println("3. Gui tien");
                System.out.println("4. Xem thong tin tai khoan");
                System.out.println("5. Thoat");
                System.out.print("Lua chon: ");
                option = in.nextInt();
                
                switch(option){
                    case 1:
                        bank.creatAccount();
                        break;
                    case 2:
                        bank.withdraw();
                        break;
                    case 3:
                        bank.deposit();
                        break;
                    case 4:
                        bank.display();
                        break;
                }
            } while(option > 0 && option < 5);
        } catch(Exception e) {
            System.out.println("Loi nhap so!");
        }
    }
}